package com.isunland.managesystem.ui;

import android.os.Bundle;
import com.google.gson.Gson;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.AppObjectOriginal;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.DictionaryTreeContent;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by lenovo on 2016/6/14.
 */
public class AppObjectDialogFragment extends BaseNetworkDialogFragment {
  /* 判断是公司还是部门对象*/
  protected static String EXTRA_TYPE =
      "com.isunland.managesystem.ui.AppObjectDialogFragment.EXTRA_TYPE";
  protected static String COMPANY = "company";
  protected static String DEPT = "dept";
  private String type = "";

  @Override public void onCreate(Bundle savedInstanceState) {
    type = getArguments().getString(EXTRA_TYPE);
    super.onCreate(savedInstanceState);
  }

  @Override public ArrayList<CustomerDialog> resolveResponse(String arg0) {
    ArrayList<CustomerDialog> list = new ArrayList<CustomerDialog>();
    AppObjectOriginal original = new Gson().fromJson(arg0, AppObjectOriginal.class);
    ArrayList<DictionaryTreeContent> dataList =
        (ArrayList<DictionaryTreeContent>) original.getRows();

    if (dataList == null || dataList.size() == 0) {

      return null;
    }
    for (int i = 0; i < dataList.size(); i++) {
      list.add(new CustomerDialog(dataList.get(i).getName(), dataList.get(i).getCustomAttrs()));
    }

    return list;
  }

  @Override public String getUrl() {
    return ApiConst.URL_APPOBJECT;
  }

  @Override public HashMap<String, String> getparameters() {

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("memCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("type", "mobile");
    params.put("async", "true");
    params.put("ifContainStaff", "false");
    params.put("orgStyles", "'001','002','003'");
    if (COMPANY.equals(type)) {
      params.put("ifNodeFlag", "T");
      params.put("ifRoleTypeFlag", "F");//是否启用角色类型
    }
    return params;
  }

  protected static AppObjectDialogFragment newInstance(String type) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_TYPE, type);
    AppObjectDialogFragment fragment = new AppObjectDialogFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
